﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Implements;

namespace Cofe.Data
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class EFHashTagConfig : IDatabaseConfigurator
    {
        public void OnDatabaseCreating(CofeDbContext context)
        {

        }

        public void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var hashTagConfig = modelBuilder.Entity<HashTag>();

            hashTagConfig.HasKey(t => t.TagName);            
            hashTagConfig.Property(t => t.TagName).HasMaxLength(64).IsRequired();
            hashTagConfig.Property(t => t.TagType).HasMaxLength(64).IsOptional();            
            hashTagConfig.ToTable("HashTag");
            hashTagConfig.HasMany(t => t.AppliedEntries)
                .WithMany(de => de.HashTags)
                .Map(mc =>
                    {
                        mc.ToTable("HashTag_Entries");
                        mc.MapLeftKey("TagName");
                        mc.MapRightKey("EntryId");
                    });
        }
    }
}
